package com.example.backend.controller;

import com.example.backend.entity.Book;

import com.example.backend.entity.Post;
import com.example.backend.mapper.BookMapper;
import com.example.backend.model.BaseModel;
import com.example.backend.model.BookModel;
import com.example.backend.service.BookService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("book")
public class BookController {
    @Resource
    private BookService bookService;

    @Resource
    HttpSession session;

    @Resource
    HttpServletRequest request;

    @RequestMapping(value = "getAllBooks", method = RequestMethod.GET)
    public BookModel getAllBooks(){
        List<Book> books =bookService.getAllBooks();
        if(books != null){
            return new BookModel(0,"success",books);
        }
        else
            return new BookModel(1,"error",null);
    }

    @RequestMapping(value = "addBooks", method = RequestMethod.POST)
    public BaseModel addBooks(){
        int bookId = Integer.parseInt(request.getParameter("bookId"));
        String bookName = request.getParameter("bookName");
        String author = request.getParameter("author");
        int num = Integer.parseInt(request.getParameter("num"));
        if(bookService.addBooks(bookId,bookName,author,num))
            return new BaseModel(0,"success");
        else
            return new BaseModel(1,"error");
    }

    @RequestMapping(value = "getBooks",method = RequestMethod.POST)
    public BookModel getBooks(){
        String bookName = request.getParameter("bookName");
        List<Book> books =bookService.getBooks(bookName);
        if(books != null)
            return new BookModel(0,"success",books);
        else
            return new BookModel(1,"error",null);
    }

    @RequestMapping(value = "deleteBooks",method = RequestMethod.POST)
    public BaseModel deleteBooks(){
        String bookName = request.getParameter("bookName");
        if(bookService.deleteBooks(bookName))
            return new BaseModel(0,"success");
        else
            return new BaseModel(1,"error");
    }

    @RequestMapping(value = "updateBooks",method = RequestMethod.POST)
    public BaseModel updateBooks(){
        String bookId = request.getParameter("bookId");
        String bookName = request.getParameter("bookName");
        String author = request.getParameter("author");
        String num = request.getParameter("num");
        Book book = new Book();
        book.setBookId(Integer.parseInt(bookId));
        book.setBookName(bookName);
        book.setAuthor(author);
        book.setNum(Integer.parseInt(num));
        if(bookService.updateBooks(book.getBookId(), book.getBookName(), book.getAuthor(), book.getNum()))
            return new BaseModel(0,"success");
        else
            return new BaseModel(1,"error");
    }
}
